package com.tma.internship.activities;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.tma.internship.ClassesInfo.CommentInfo;
import com.tma.internship.ClassesInfo.ControlBookMark;
import com.tma.internship.ClassesInfo.FileInfo;
import com.tma.internship.ClassesInfo.ImageAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class StoredFilesActivity extends Activity implements OnItemClickListener{
	private List<FileInfo> listFileInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridviewimage);
		hashFInfo = new HashMap<String, FileInfo>();
		GridView gridview = (GridView) findViewById(R.id.gridview);
		FileInputStream config;
		ObjectInputStream oisConfig;
		listFileInfo = null;
		try {
			config = openFileInput("config");
			oisConfig = new ObjectInputStream(config);
			listFileInfo = (List<FileInfo>) oisConfig.readObject();
		} catch (Exception e){
			e.printStackTrace();
		}
		if (listFileInfo == null) 
		{
			ConfirmDownloadDlg dlg = new ConfirmDownloadDlg(this){
				@Override
				public void onOK() {
					// TODO Auto-generated method stub
					isResume = true;
				}
			};
			dlg.show();
			return;
		}
		List<Bitmap> lsBitmap = new ArrayList<Bitmap>();
		for (FileInfo tmp:listFileInfo){
			try {
				hashFInfo.put(tmp.getName(), tmp);
				FileInputStream fin = openFileInput(tmp.getName()+"_"+0+".png");
				BitmapDrawable bmd = new BitmapDrawable(fin);
				Bitmap bm = bmd.getBitmap();
				if ( bm == null) continue;
				lsBitmap.add(bm);
			} catch (Exception e){
				e.printStackTrace();
			}
		}		
		ImageAdapter imgAdapter = new ImageAdapter(this, lsBitmap, R.layout.itemimage, listFileInfo);
		gridview.setAdapter(imgAdapter);
		gridview.setOnItemClickListener(this);
	}
	private HashMap<String,FileInfo> hashFInfo;
	private boolean isResume = false;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isResume) {
			setContentView(R.layout.gridviewimage);
			hashFInfo = new HashMap<String, FileInfo>();
			GridView gridview = (GridView) findViewById(R.id.gridview);
			FileInputStream config;
			ObjectInputStream oisConfig;
			listFileInfo = null;
			try {
				config = openFileInput("config");
				oisConfig = new ObjectInputStream(config);
				listFileInfo = (List<FileInfo>) oisConfig.readObject();
			} catch (Exception e){
				
			}
			if (listFileInfo == null) return;
			List<Bitmap> lsBitmap = new ArrayList<Bitmap>();
			for (FileInfo tmp:listFileInfo){
				try {
					hashFInfo.put(tmp.getName(), tmp);
					FileInputStream fin = openFileInput(tmp.getName()+"_"+0+".png");
					BitmapDrawable bmd = new BitmapDrawable(fin);
					Bitmap bm = bmd.getBitmap();
					if ( bm == null) continue;
					lsBitmap.add(bm);
				} catch (Exception e){
					e.printStackTrace();
				}
			}		
			ImageAdapter imgAdapter = new ImageAdapter(this, lsBitmap, R.layout.itemimage, listFileInfo);
			gridview.setAdapter(imgAdapter);
			gridview.setOnItemClickListener(this);
			isResume = false;
		}
		
	}


	@Override
	public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		FileInfo fInfo = (FileInfo) adapter.getItemAtPosition(position);
		Intent i = new Intent(this,ViewBookActivity.class);
		i.putExtra("file", fInfo);
		startActivity(i);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.list_file_menu, menu);
		return true;
	}


	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()){
		case R.id.compare_server:
			Intent i = new Intent(this, ListFileActivity.class);
			HashMap<String,FileInfo> hashMapFile = new HashMap<String, FileInfo>();
			if (listFileInfo != null){
				for (FileInfo tmp:listFileInfo)
					hashMapFile.put(tmp.getName(), tmp);
				i.putExtra("listFileInfo", (Serializable) hashMapFile);			}
			startActivity(i);
			isResume = true;
			return true;
		case R.id.list_bookmark:
			ControlBookMark ctr = new ControlBookMark(this);
			List<CommentInfo> listCmtInfos = ctr.listCmtAll();
			Intent intent = new Intent(this,BookmarkActivity.class);
			//Log.d("list", listCmtInfos.get(0).getTime());
			if (listCmtInfos!= null) for (CommentInfo cmt:listCmtInfos){
				cmt.setfInfo(hashFInfo.get(cmt.getBookName()));
			}
			intent.putExtra("list", (Serializable) listCmtInfos);
			startActivity(intent);
			return true;
		case R.id.exit:
			AlertDialog.Builder b = new AlertDialog.Builder(this);
			b.setTitle(getString(R.string.title_exit));
			b.setMessage(getString(R.string.confirm_exit));
			b.setPositiveButton(android.R.string.yes, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
			});
			b.setNegativeButton(android.R.string.no, null);
			b.show();
		default:return super.onMenuItemSelected(featureId, item);
		}
		
	}
	
}
